package com.taichuan.selfcheck.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.taichuan.selfcheck.R;
import com.taichuan.selfcheck.config.PropertyDefaultValue;
import com.taichuan.selfcheck.entity.WifiBean;
import com.taichuan.selfcheck.mvp.presenter.WifiPresenter;
import com.taichuan.selfcheck.mvp.viewinterface.WifiInterface;
import com.taichuan.selfcheck.ui.adapter.WifiInfoAdapter;
import com.taichuan.selfcheck.ui.dialog.ResultDialog;
import com.taichuan.selfcheck.ui.fragment.base.AppBaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 该类主要用于
 *
 * @author CamelLuo
 * @version 2019/12/4
 */
public class WiFiFragment extends AppBaseFragment<WifiInterface, WifiPresenter> implements WifiInterface {

    @BindView(R.id.wifi_open)
    Button wifiOpenBtn;
    @BindView(R.id.wifi_close)
    TextView wifiCloseTv;

    @BindView(R.id.fragment_wifi_recyclerView)
    RecyclerView recyclerView;

    ResultDialog resultDialog;

    public int wifiInitTime = 3;       //倒计时
    @BindView(R.id.txt_wifi_listempty)
    TextView emptyTv;
    private boolean startInit = false;  //只用一次倒计时
    public static boolean wifiOpenAgain = false;
    private WifiInfoAdapter wifiInfoAdapter;



    @Override
    protected void getBundleValues(Bundle bundle) {

    }

    @Override
    protected int setContentViewId() {
        return R.layout.fragment_wifi;
    }

    @Override
    protected void initView(View view, Bundle bundle) {
        initRecyclerView();

        resultDialog = new ResultDialog(PropertyDefaultValue.FUNCTION_TEST_6,getResources().getString(R.string.wifi),getContext());       //设置弹窗发送的事件——WIFI测试
        if(mPresenter.checkWifi()){
            wifiCloseTv.setText("wifi搜索中");
            wifiOpenBtn.setText(getStrById(R.string.close_wifi));
            wifiOpenAgain=true;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        handler.removeMessages(1);
        mPresenter.closeWIFI();
    }

    private void initRecyclerView() {
        wifiInfoAdapter = new WifiInfoAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(wifiInfoAdapter);
    }

    @OnClick({R.id.wifi_open, R.id.wifi_close})
    @Override
    protected void onXmlClick(View v) {
        super.onXmlClick(v);
        switch (v.getId()) {
            case R.id.wifi_open: {
                wifiOpenAgain=!wifiOpenAgain;
                if(wifiOpenAgain){
                    wifiOpenBtn.setText(getStrById(R.string.close_wifi));
                    mPresenter.openWiFi();
                    handler.sendEmptyMessage(1);
                    wifiOpenBtn.setClickable(false);
                }else{
                    wifiOpenBtn.setText(getStrById(R.string.open_wifi));
                    if(handler.hasMessages(1)){
                        handler.removeMessages(1);
                    }
                    wifiCloseTv.setText("wifi未开启 ");
                    mPresenter.closeWIFI();
                    wifiInfoAdapter.setNewData(new ArrayList<>());
                    emptyTv.setVisibility(View.GONE);
                }
                break;
            }
        }
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            switch (message.what) {
                case 1:
                    if (wifiInitTime-- > 0) {
                        wifiCloseTv.setText("wifi开启中 " );
                        handler.sendEmptyMessageDelayed(1, 1000);
                    } else {
                        mPresenter.getInfo();
                        wifiInitTime=3;
                        wifiOpenBtn.setClickable(true);
                    }
                    break;
            }
            return false;
        }
    });

    @Override
    protected WifiPresenter createPresenter() {
        return new WifiPresenter(getActivity());
    }

    @Override
    public void showWIFI(List<WifiBean> wifiBeanList) {
        wifiInfoAdapter.setNewData(wifiBeanList);
        wifiCloseTv.setText("wifi已打开");
        if (wifiBeanList!=null && wifiBeanList.size()>0) {
            emptyTv.setVisibility(View.GONE);
        } else {
            emptyTv.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void changeButtonText(String status) {
//        wifiOpenBtn.setText(status);
        wifiInitTime=2;
        handler.sendEmptyMessageDelayed(1, 1000);
    }

    @Override
    protected boolean isAllowAutoFinishFragment() {
        return false;
    }

    @Override
    public void doOnAutoFinishFragment() {
        resultDialog.show();
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (resultDialog.isShowing()){
            resultDialog.dispatchKeyEvent(event);
        }
        if (event.getKeyCode() == KeyEvent.KEYCODE_POUND){
            wifiOpenBtn.performClick();
        }
        return super.dispatchKeyEvent(event);
    }
}
